<?php
Yii::import('application.components.Reference');
Yii::import('application.components.U');
class PelunasanUtangController extends GxController
{
    public function actionCreate()
    {
        if (!Yii::app()->request->isAjaxRequest)
            $this->redirect(url('/'));
        if (isset($_POST) && !empty($_POST)) {
            Yii::import('application.components.Gl');
            $gl = new Gl();
            $msg = 'Pelunasan utang berhasil disimpan.';
            $detils = CJSON::decode($_POST['detil']);
            app()->db->autoCommit = false;
            $transaction = Yii::app()->db->beginTransaction();
            try {
                $pelunasan = new PelunasanUtang;
                $docref = $pelunasan->doc_ref;
                $ref = new Reference();
                $docref = $ref->get_next_reference(PELUNASANUTANG);
                foreach ($_POST as $k => $v) {
                    if (is_angka($v)) $v = get_number($v);
                    $_POST['PelunasanUtang'][$k] = $v;
                }
                $_POST['PelunasanUtang']['doc_ref'] = $docref;
                $pelunasan->attributes = $_POST['PelunasanUtang'];
                if (!$pelunasan->save())
                    throw new Exception("Gagal menyimpan pelunasan utang. " . CHtml::errorSummary($pelunasan));
                PelunasanUtangDetil::model()->deleteAll('pelunasan_utang_id  = :pelunasan_utang_id',
                    array(':pelunasan_utang_id' => $pelunasan->pelunasan_utang_id));
                foreach ($detils as $detil) {
                    $pelunasan_utang_detil = new PelunasanUtangDetil;
                    $_POST['PelunasanUtangDetil']['mode_id'] = $detil['id'];
                    $_POST['PelunasanUtangDetil']['trans_mode'] = $detil['trans_mode'];
                    $_POST['PelunasanUtangDetil']['kas_dibayar'] = get_number($detil['kas_dibayar']);
                    $_POST['PelunasanUtangDetil']['sisa'] = get_number($detil['sisa']);
                    $_POST['PelunasanUtangDetil']['pelunasan_utang_id'] = $pelunasan->pelunasan_utang_id;
                    $pelunasan_utang_detil->attributes = $_POST['PelunasanUtangDetil'];
                    if (!$pelunasan_utang_detil->save())
                        throw new Exception("Gagal menyimpan detil pelunasan utang. " . CHtml::errorSummary($pelunasan_utang_detil));
                    if ($_POST['PelunasanUtangDetil']['kas_dibayar'] == get_number($detil['sisa'])) {
                        // $trans_model :
                        //          0 = pembelian ato retur pembelian
                        //          1 = nota debet
                        $trans_model = ($pelunasan_utang_detil->trans_mode == 0) ? "Pembelian" : "NotaDebet";
                        $trans = $this->loadModel($pelunasan_utang_detil->mode_id, $trans_model);
                        $trans->lunas = 1;
                        if (!$trans->save())
                            throw new Exception("Gagal mengubah status final. " . CHtml::errorSummary($trans));
                    }
                }
                $ref->save(PELUNASANUTANG, $pelunasan->pelunasan_utang_id, $docref);
                //Utang Debet
                $gl->add_gl(PELUNASANUTANG, $pelunasan->pelunasan_utang_id, $pelunasan->tgl, $pelunasan->doc_ref,
                    UTANGDAGANG, '', '', $pelunasan->total);
                //Kas Kredit
                $gl->add_gl(PELUNASANUTANG, $pelunasan->pelunasan_utang_id, $pelunasan->tgl, $pelunasan->doc_ref,
                    $pelunasan->idBank->account_code, '', '', -$pelunasan->total);
                $gl->validate();
                $transaction->commit();
                $status = true;
            } catch (Exception $ex) {
                $transaction->rollback();
                $status = false;
                $msg = $ex->getMessage();
            }
            echo CJSON::encode(array(
                'success' => $status,
                'id' => $docref,
                'msg' => $msg));
            Yii::app()->end();
        }
    }
    public function actionIndex()
    {
//        if (isset($_POST['limit'])) {
//            $limit = $_POST['limit'];
//        } else {
//            $limit = 20;
//        }
//        if (isset($_POST['start'])) {
//            $start = $_POST['start'];
//        } else {
//            $start = 0;
//        }
        $criteria = new CDbCriteria();
        $criteria->addCondition('tgl = :tgl');
        $criteria->params = array(':tgl'=>$_POST['tgl']);
//        if ((isset ($_POST['mode']) && $_POST['mode'] == 'grid') ||
//            (isset($_POST['limit']) && isset($_POST['start']))
//        ) {
//            $criteria->limit = $limit;
//            $criteria->offset = $start;
//        }
        $model = PelunasanUtang::model()->findAll($criteria);
        $total = PelunasanUtang::model()->count($criteria);
        $this->renderJson($model, $total);
    }
}